﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using bts_webapp.Models;
using Telerik.Web.UI;
using System.Drawing;

namespace bts_webapp
{
    public partial class UxFlow : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                
            }
            else
            {
                               
            }
        }

        public void comboboxTramDiSelectedIndexChanged(object obj, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            this.SelectThietbidi(obj,e);
            this.SelectCarddi(obj, e);
            this.SelectPortdi(obj, e);
        }

        public void comboboxThietbiDiSelectedIndexChanged(object obj, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            this.SelectCarddi(obj, e);
            this.SelectPortdi(obj, e);
        }

        public void comboboxCardDiSelectedIndexChanged(object obj, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            this.SelectPortdi(obj, e);
        }

        public void comboboxTramDenSelectedIndexChanged(object obj, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            this.SelectThietbiden(obj, e);
            this.SelectCardden(obj, e);
            this.SelectPortden(obj, e);
        }

        public void comboboxThietbiDenSelectedIndexChanged(object obj, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            this.SelectCardden(obj, e);
            this.SelectPortden(obj, e);
        }

        public void comboboxCardDenSelectedIndexChanged(object obj, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            this.SelectPortden(obj, e);
        }

        public string FlowId
        {
            get
            {
                return this.txtFlowId.Text.Trim();
            }
            set
            {
                this.txtFlowId.Text = value;
            }
        }

        public string FlowName
        {
            get
            {
                return this.txtFlowName.Text.Trim();
            }
            set { this.txtFlowName.Text = value; }
        }

        public string Tramden
        {
            get
            {
                return this.comboTramDen.SelectedItem.Value.ToString().Trim();
            }
            set { this.UpdateFlow_TramDen(value); }
        }

        public string Tramdi
        {
            get
            {
                return this.comboTramDi.SelectedItem.Value.ToString().Trim();
            }
            set { this.UpdateFlow_TramDi(value); }
        }

        public string Portden
        {
            get
            {
                return this.comboPortden.SelectedItem.Value.ToString().Trim();
            }
            set { this.UpdateFlow_PortDen(value); }
        }

        public string Portdi
        {
            get
            {
                return this.comboPortdi.SelectedItem.Value.ToString().Trim();
            }
            set { this.UpdateFlow_PortDi(value); }
        }

        public string ThietbiDi
        {
            get
            {
                return this.comboThietbiDi.SelectedItem.Value.ToString().Trim();
            }
            set { this.UpdateFlow_ThietbiDi(value); }
        }

        public string ThietbiDen
        {
            get
            {
                return this.comboThietbiDen.SelectedItem.Value.ToString().Trim();
            }
            set { this.UpdateFlow_ThietbiDen(value); }
        }

        public string CardDi
        {
            get
            {
                return this.comboCardDi.SelectedItem.Value.ToString().Trim();
            }
            set { this.UpdateFlow_CardDi(value); }
        }

        public string CardDen
        {
            get
            {
                return this.comboCardDen.SelectedItem.Value.ToString().Trim();
            }
            set { this.UpdateFlow_CardDen(value); }
        }

        public string Description
        {
            get
            {
                return this.txtGhichu.Text.Trim();
            }
            set { this.txtGhichu.Text = value; }
        }

        public bool Status
        {
            get
            {
                return this.chkStatus.Checked;
            }
            set { this.chkStatus.Checked = value; }
        }

        public string Lichsu
        {
            get
            {
                return this.txtLichSu.Text.Trim();
            }
            set { this.txtLichSu.Text = value; }
        }

        public bool InsertOrUpdate
        {
            set
            {
                this.btnUpdate.CommandName = value ? "PerformInsert" : "Update";
                this.btnUpdate.Text = value ? "Insert" : "Update";
            }
            get
            {
                return this.btnUpdate.Text == "Insert";
            }
        }


       
        public void BindData(VIBA_LUONG flow)
        {
            this.FlowId = flow.Luong_Id;
            this.FlowName = flow.Luong_Ten.ToString().Trim();
            this.Portden = Convert.ToString(flow.Luong_Port_Den).Trim();
            this.Portdi=Convert.ToString(flow.Luong_Port_Di).Trim ();
            this.Tramden = flow.Luong_Id_Tram_Den.ToString().Trim();
            this.Tramdi = flow.Luong_Id_Tram_Di.ToString().Trim();
            this.Lichsu = flow.Luong_Lich_Su_Duong_Di.ToString().Trim();
            this.Description = Convert.ToString(flow.Luong_GhiChu).Trim ();
            this.Status = flow.Status == 0 ? false : true;
            this.ThietbiDi = IsDBNull(flow.Luong_ThietBi_Di).Trim();
            this.ThietbiDen = IsDBNull(flow.Luong_ThietBi_Den).Trim();
            this.CardDi = IsDBNull(flow.Luong_Card_Di.ToString()).Trim();
            this.CardDen = IsDBNull(flow.Luong_Card_Den.ToString()).Trim();
        }

        public void EnableFieldCode(bool enabled)
        {
            this.txtFlowId.Enabled = enabled;
        }

        public void SetFieldCode(bool enabled)
        {
            this.txtFlowId.Enabled = enabled;
        }

       public VIBA_LUONG GetData()
        {
            VIBA_LUONG flow = new VIBA_LUONG
            {
                Luong_Id = this.InsertOrUpdate ? Guid.NewGuid().ToString(): this.FlowId,
                Luong_Ten = this.FlowName,
                Luong_Id_Tram_Den = this.Tramden,
                Luong_Id_Tram_Di = this.Tramdi,
                Luong_Port_Den = Convert.ToInt32(this.Portden),
                Luong_Port_Di = Convert.ToInt32( this.Portdi),
                Luong_Lich_Su_Duong_Di = this.Lichsu,
                Luong_GhiChu = this.Description,
                Status = GetStatus(this.Status),
                Luong_ThietBi_Di=this.ThietbiDi,
                Luong_ThietBi_Den = this.ThietbiDen,
                Luong_Card_Di = Convert.ToInt32(this.CardDi),
                Luong_Card_Den = Convert.ToInt32(this.CardDen)
            };
            return flow;
        }

        private int GetStatus(bool status)
        {
            return status ? 1 : 0;
        }

        public void SetErrorStatus(string message)
        {
            if (message.Trim() != string.Empty)
                this.litErrStatus.Text = "<font color='red'>" + message + "</font>";
            else
                this.litErrStatus.Text = string.Empty;
        }

        public void AddComboBoxItems()
        {
            using (var db = new BTSModelDataContext())
            {
                var list = db.VIBA_TRAMs;
                this.comboTramDi.DataSource = list;
                this.comboTramDi.DataTextField = "Tram_Ten";
                this.comboTramDi.DataValueField = "Tram_Id";
                this.comboTramDi.DataBind();
                this.UpdateFlow_TramDi(this.Tramdi);
                
            }
        }

        public void AddComboBoxItems_TramDen()
        {
            using (var db = new BTSModelDataContext())
            {
                var list = db.VIBA_TRAMs;
                this.comboTramDen.DataSource = list;
                this.comboTramDen.DataTextField = "Tram_Ten";
                this.comboTramDen.DataValueField = "Tram_Id";
                this.comboTramDen.DataBind();
                this.UpdateFlow_TramDen(this.Tramden);
            }
        }
        

        public void AddComboBoxItems_ThietbiDi(string TramId_Di)
        {
            using (var db = new BTSModelDataContext())
            {
                var list = from tb in db.VIBA_THIET_BIs where tb.Tram_Id == TramId_Di select tb;
                this.comboThietbiDi.DataSource = list;
                this.comboThietbiDi.DataTextField = "ThietBi_Ten";
                this.comboThietbiDi.DataValueField = "ThietBi_Id";
                this.comboThietbiDi.DataBind();
                this.UpdateFlow_ThietbiDi(this.ThietbiDi);
            }
        }

        public void AddComboBoxItems_ThietbiDi()
        {
            using (var db = new BTSModelDataContext())
            {
                var list = from tb in db.VIBA_THIET_BIs select tb;
                this.comboThietbiDi.DataSource = list;
                this.comboThietbiDi.DataTextField = "ThietBi_Ten";
                this.comboThietbiDi.DataValueField = "ThietBi_Id";
                this.comboThietbiDi.DataBind();
                this.UpdateFlow_ThietbiDi(this.ThietbiDi);
            }
        }

        public void AddComboBoxItems_ThietbiDen(string TramId_Den)
        {
            using (var db = new BTSModelDataContext())
            {
                var list = from tb in db.VIBA_THIET_BIs where tb.Tram_Id == TramId_Den select tb;
                this.comboThietbiDen.DataSource = list;
                this.comboThietbiDen.DataTextField = "ThietBi_Ten";
                this.comboThietbiDen.DataValueField = "ThietBi_Id";
                this.comboThietbiDen.DataBind();
                this.UpdateFlow_ThietbiDen(this.ThietbiDen);
            }
        }

        public void AddComboBoxItems_ThietbiDen()
        {
            using (var db = new BTSModelDataContext())
            {
                var list = from tb in db.VIBA_THIET_BIs select tb;
                this.comboThietbiDen.DataSource = list;
                this.comboThietbiDen.DataTextField = "ThietBi_Ten";
                this.comboThietbiDen.DataValueField = "ThietBi_Id";
                this.comboThietbiDen.DataBind();
                this.UpdateFlow_ThietbiDen(this.ThietbiDen);
            }
        }

        public void AddComboBoxItems_CardDi(string ThietbiId_Di)
        {
            using (var db = new BTSModelDataContext())
            {
                var list = from ca in db.VIBA_CARDs where ca.ThietBi_Id == ThietbiId_Di select ca;
                this.comboCardDi.DataSource = list;
                this.comboCardDi.DataTextField = "Card_TenCard";
                this.comboCardDi.DataValueField = "Card_Id";
                this.comboCardDi.DataBind();
                this.UpdateFlow_CardDi(this.CardDi);
            }
        }

        public void AddComboBoxItems_CardDi()
        {
            using (var db = new BTSModelDataContext())
            {
                var list = from ca in db.VIBA_CARDs select ca;
                this.comboCardDi.DataSource = list;
                this.comboCardDi.DataTextField = "Card_TenCard";
                this.comboCardDi.DataValueField = "Card_Id";
                this.comboCardDi.DataBind();
                this.UpdateFlow_CardDi(this.CardDi);
            }
        }

        public void AddComboBoxItems_CardDen()
        {
            using (var db = new BTSModelDataContext())
            {
                var list = from ca in db.VIBA_CARDs select ca;
                this.comboCardDen.DataSource = list;
                this.comboCardDen.DataTextField = "Card_TenCard";
                this.comboCardDen.DataValueField = "Card_Id";
                this.comboCardDen.DataBind();
                this.UpdateFlow_CardDen(this.CardDen);
            }
        }

        public void AddComboBoxItems_CardDen(string ThietbiId_Den)
        {
            using (var db = new BTSModelDataContext())
            {
                var list = from ca in db.VIBA_CARDs where ca.ThietBi_Id == ThietbiId_Den select ca;
                this.comboCardDen.DataSource = list;
                this.comboCardDen.DataTextField = "Card_TenCard";
                this.comboCardDen.DataValueField = "Card_Id";
                this.comboCardDen.DataBind();
                this.UpdateFlow_CardDen(this.CardDen);
            }
        }

        public void AddComboBoxItems_PortDi()
        {
            using (var db = new BTSModelDataContext())
            {
                var list = from po in db.VIBA_PORTs select po;
                this.comboPortdi.DataSource = list;
                this.comboPortdi.DataTextField = "Port_Number";
                this.comboPortdi.DataValueField = "Port_Number";
                this.comboPortdi.DataBind();
                this.UpdateFlow_PortDi(this.Portdi);
            }
        }

        public void AddComboBoxItems_PortDi(String CardId_Di)
        {
            using (var db = new BTSModelDataContext())
            {
                var list = from po in db.VIBA_PORTs where po.Card_Id.ToString() == CardId_Di select po;
                this.comboPortdi.DataSource = list;
                this.comboPortdi.DataTextField = "Port_Number";
                this.comboPortdi.DataValueField = "Port_Number";
                this.comboPortdi.DataBind();
                this.UpdateFlow_PortDi(this.Portdi);
            }
        }

        public void AddComboBoxItems_PortDen()
        {
            using (var db = new BTSModelDataContext())
            {
                var list =from po in db.VIBA_PORTs select po;
                this.comboPortden.DataSource = list;
                this.comboPortden.DataTextField = "Port_Number";
                this.comboPortden.DataValueField = "Port_Number";
                this.comboPortden.DataBind();
                this.UpdateFlow_PortDen(this.Portden);
            }
        }

        public void AddComboBoxItems_PortDen(string CardId_Den)
        {
            using (var db = new BTSModelDataContext())
            {
                var list = from po in db.VIBA_PORTs where po.Card_Id.ToString() == CardId_Den select po;
                this.comboPortden.DataSource = list;
                this.comboPortden.DataTextField = "Port_Number";
                this.comboPortden.DataValueField = "Port_Number";
                this.comboPortden.DataBind();
                this.UpdateFlow_PortDen(this.Portden);
            }
        }

        private void UpdateFlow_TramDi(string TramDi_Id)
        {
            var item = this.comboTramDi.Items.FirstOrDefault(x => x.Value.Trim() == TramDi_Id.Trim());
            if (item != null)
            {
                var index = this.comboTramDi.Items.IndexOf(item);
                this.comboTramDi.SelectedIndex = index;
            }
        }

        private void UpdateFlow_TramDen(string TramDen_Id)
        {
            var item = this.comboTramDen.Items.FirstOrDefault(x => x.Value.Trim() == TramDen_Id.Trim());
            if (item != null)
            {
                var index = this.comboTramDen.Items.IndexOf(item);
                this.comboTramDen.SelectedIndex = index;
            }
        }

        private void UpdateFlow_ThietbiDi(string Thietbidi_Id)
        {
            var item = this.comboThietbiDi.Items.FirstOrDefault(x => x.Value.Trim() == Thietbidi_Id.Trim());
            if (item != null)
            {
                var index = this.comboThietbiDi.Items.IndexOf(item);
                this.comboThietbiDi.SelectedIndex = index;
            }
        }
        //private void comboTramDen_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        //{
        //    string TramDenID = comboTramDen.SelectedItem.ToString().Trim();
        //    UpdateFlow_ThietbiDen(TramDenID);
        //}
        private void UpdateFlow_ThietbiDen(string Thietbiden_Id)
        {
            var item = this.comboThietbiDen.Items.FirstOrDefault(x => x.Value.Trim() == Thietbiden_Id.Trim());
            if (item != null)
            {
                var index = this.comboThietbiDen.Items.IndexOf(item);
                this.comboThietbiDen.SelectedIndex = index;
            }
        }

        private void UpdateFlow_CardDi(string carddi_Id)
        {
            var item = this.comboCardDi.Items.FirstOrDefault(x => x.Value.Trim() == carddi_Id.Trim());
            if (item != null)
            {
                var index = this.comboCardDi.Items.IndexOf(item);
                this.comboCardDi.SelectedIndex = index;
            }
        }

        private void UpdateFlow_CardDen(string cardden_Id)
        {
            var item = this.comboCardDen.Items.FirstOrDefault(x => x.Value.Trim() == cardden_Id.Trim());
            if (item != null)
            {
                var index = this.comboCardDen.Items.IndexOf(item);
                this.comboCardDen.SelectedIndex = index;
            }
        }

        private void UpdateFlow_PortDi(string portdi_Id)
        {
            var item = this.comboPortdi.Items.FirstOrDefault(x => x.Value.Trim() == portdi_Id.Trim());
            if (item != null)
            {
                var index = this.comboPortdi.Items.IndexOf(item);
                this.comboPortdi.SelectedIndex = index;
            }
        }

        private void UpdateFlow_PortDen(string portden_Id)
        {
            var item = this.comboPortden.Items.FirstOrDefault(x => x.Value.Trim() == portden_Id.Trim());
            if (item != null)
            {
                var index = this.comboPortden.Items.IndexOf(item);
                this.comboPortden.SelectedIndex = index;
            }
        }

        public string IsDBNull(string st)
        {
            if (st == null)
                return st=  "";
            return st;
        }

       //is postback data in combobox

        public void SelectThietbidi(Object obj, EventArgs e)
        {
            var bts = new BTSModelDataContext();
            var tblistdi = from tb in bts.VIBA_THIET_BIs where tb.Tram_Id == this.comboTramDi.SelectedValue select tb;
            this.comboThietbiDi.DataSource = tblistdi;
            this.comboThietbiDi.DataTextField = "ThietBi_Ten";
            this.comboThietbiDi.DataValueField = "ThietBi_id";
            this.comboThietbiDi.DataBind();        
        }

        public void SelectThietbiden(Object obj, EventArgs e)
        {
            var bts = new BTSModelDataContext();
            var tblistden = from tb in bts.VIBA_THIET_BIs where tb.Tram_Id == this.comboTramDen.SelectedValue.ToString() select tb;
            this.comboThietbiDen.DataSource = tblistden;
            this.comboThietbiDen.DataTextField = "ThietBi_Ten";
            this.comboThietbiDen.DataValueField = "ThietBi_id";
            this.comboThietbiDen.DataBind();
        }

        public void SelectCarddi(Object obj, EventArgs e)
        {
            var bts = new BTSModelDataContext();
            var tbcard = from ca in bts.VIBA_CARDs where ca.ThietBi_Id.ToString() == this.comboThietbiDi.SelectedValue select ca;
            this.comboCardDi.DataSource = tbcard;
            this.comboCardDi.DataTextField = "Card_TenCard";
            this.comboCardDi.DataValueField = "Card_id";
            this.comboCardDi.DataBind();
        }

        public void SelectCardden(Object obj, EventArgs e)
        {
            var bts = new BTSModelDataContext();
            var tbcardden = from ca in bts.VIBA_CARDs where ca.ThietBi_Id.ToString() == this.comboThietbiDen.SelectedValue.ToString() select ca;
            this.comboCardDen.DataSource = tbcardden;
            this.comboCardDen.DataTextField = "Card_TenCard";
            this.comboCardDen.DataValueField = "Card_id";
            this.comboCardDen.DataBind();
        }

        public void SelectPortdi(Object obj, EventArgs e)
        {
            var bts = new BTSModelDataContext();
            var tbport = from po in bts.VIBA_PORTs where po.Card_Id.ToString() == this.comboCardDi.SelectedValue.ToString() select po;
            this.comboPortdi.DataSource = tbport;
            this.comboPortdi.DataTextField = "Port_Number";
            this.comboPortdi.DataValueField = "Port_Number";
            this.comboPortdi.DataBind();
        }

        public void SelectPortden(Object obj, EventArgs e)
        {
            var bts = new BTSModelDataContext();
            var tbportden = from po in bts.VIBA_PORTs where po.Card_Id.ToString() == this.comboCardDen.SelectedValue.ToString() select po;
            this.comboPortden.DataSource = tbportden;
            this.comboPortden.DataTextField = "Port_Number";
            this.comboPortden.DataValueField = "Port_Number";
            this.comboPortden.DataBind();
        }

        public void SelectCombo(Object obj, EventArgs e)
        {
            if (comboTramDi.IsCaseSensitive)
            {
                SelectThietbidi(obj, e);
                SelectCarddi(obj, e);
                SelectPortdi(obj, e);
            }
            if (comboThietbiDi.IsCaseSensitive)
            {
                SelectCarddi(obj, e);
                SelectPortdi(obj, e);
            }
            if (comboCardDi.IsCaseSensitive)
            {
                SelectPortdi(obj, e);
            }
        }
    }
}